// Author: Bjorn Schobben

#include <algorithm>
#include "Properties.h"

using std::min;
using std::max;

Properties::Properties(int newColorCount): colorCount(newColorCount)
{
	InitializeDefaultColors();
}

Properties::Properties(const Properties& rhs)
{
	Copy(rhs);
}

Properties::~Properties()
{
}

//Return the amount of colors currently set
unsigned int Properties::GetColorCount() const
{
	return colorCount;
}

//Set the amount of colors that can be used
void Properties::SetColorCount(unsigned int newColorCount)
{
	colorCount = min(max(newColorCount, MINCOLOR), MAXCOLOR);
}

//Copy a Properties object into this object
void Properties::Copy(const Properties& properties)
{
	colorCount = properties.colorCount;

	for(unsigned int i=0; i<MAXCOLOR; i++)
	{
		colors[i] = properties.colors[i];
	}
}

//Initialize the list of colors with preset default colors
void Properties::InitializeDefaultColors()
{
	for(unsigned int i=0; i<MAXCOLOR; i++)
	{
		colors[i] = DefaultColor(i);
	}
}

//Return the color at index 
Color Properties::GetColor(int index) const
{
	return colors[index];
}

//Set a color at index
void Properties::SetColor(int index, Color color)
{
	colors[index] = color;
}


// Test if the colors in the table are all unique
bool Properties::IsColorsUnique() const
{
	for(unsigned int i=0; i<MAXCOLOR; i++)
	{
		for(unsigned int j=0; j<MAXCOLOR; j++)
		{
			if (i != j && colors[i] == colors[j])
			{
				return false;
			}
		}
	}

	return true;
}

//Returns the list of the preset default colors
Color Properties::DefaultColor(int i) const
{
	static const Color defaultColors[MAXCOLOR]=
	{
		Color(0xFF0301), // Red
		Color(0xFFFC00), // Yellow
		Color(0x1EFF01), // Green
		Color(0x012AFF), // Blue
		Color(0x741364), // Purple
		Color(0xFF9600), // Orange
	};

	return defaultColors[i];
}

//Overloaded operator = for properties
Properties& Properties::operator=(const Properties& rhs)
{
	if (this != &rhs)
	{
		Copy(rhs);
	}

	return *this;
}


